package com.yueya.basic.news.content.service;

import java.util.Map;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.yueya.basic.news.content.model.Content;
import com.yueya.basic.news.content.model.ContentText;
import com.yueya.common.util.DateUtil;
import com.yueya.common.util.UUIDGenerator;

/**
 * 
 * 
 * @author yangfeng
 * 
 */
public class ContentService {

	@Before(Tx.class)
	public boolean save(Content c, String content, String contentText) {
		boolean bl = false;
		boolean bal = true;
		if (c != null && c.getStr("content_id") != null && !"".equals(c.getStr("content_id"))) {
			ContentText ct = ContentText.dao.findByContentId(c.getStr("content_id"));
			if (ct != null) {
				ct.set("content_html", content).set("content_text", contentText).set("update_time",
						DateUtil.getDate(DateUtil.timeFormat));
				bl = ct.update();
			} else {
				ct = new ContentText().set("content_text_id", UUIDGenerator.getUUID())
						.set("content_id", c.get("content_id")).set("content_html", content)
						.set("content_text", contentText).set("create_time", DateUtil.getDate(DateUtil.timeFormat));

				bl = ct.save();
			}

			if (bl) {
				c.set("update_time", DateUtil.getDate(DateUtil.timeFormat));
				bl = c.update();
			}

		} else {
			int maxOrderNum = Content.dao.getMaxOrderNum();
			c.set("content_id", UUIDGenerator.getUUID());
			c.set("order_num", maxOrderNum + 1);
			c.set("create_time", DateUtil.getDate(DateUtil.timeFormat));
			bl = c.save();
			if (bl) {
				ContentText ct = new ContentText().set("content_text_id", UUIDGenerator.getUUID())
						.set("content_id", c.get("content_id")).set("content_html", content)
						.set("content_text", contentText).set("create_time", DateUtil.getDate(DateUtil.timeFormat));

				bl = ct.save();
			}

		}

		return bl && bal;
	}

	public Page<Record> pagination(int pageNumber, int pageSize, Map<String, Object> map) {

		String select = "select a.*,b.user_name username,c.subject_name subjectname ";
		StringBuffer sb = new StringBuffer(
				" from t_content a left join t_user b on a.user_id=b.user_id left join t_subject c on a.subject_id=c.subject_id where a.del_flag='0' ");

		if (map.get("flag") != null && "1".equals(map.get("flag"))) {
			sb.append(" and a.user_id = '" + map.get("userid") + "' ");
		}

		if (map.get("name") != null && !"".equals(map.get("name"))) {
			sb.append(" and  a.title like '%" + map.get("name") + "%'  ");
		}
		if (map.get("subjectid") != null && !"".equals(map.get("subjectid"))) {
			sb.append(" and a.subject_id = '" + map.get("subjectid") + "' ");
		}
		
		sb.append("  order by a.is_top,a.is_essence desc,a.order_num,a.create_time desc ");

		String sql = sb.toString();
		System.out.println(sql);
		Page<Record> page = Db.paginate(pageNumber, pageSize, select, sql);

		return page;

	}


	public Record getContentById(String id) {
		String sql = "select a.*,b.user_name usernam,c.content_html,c.content_text "
				+ "from t_content  a left join t_user b on a.user_id=b.user_id "
				+ "left join t_content_text c on a.content_id=c.content_id where  a.content_id=? "
				+ "order by a.create_time desc";
		Record r = Db.findFirst(sql, id);

		return r;
	}

}
